package com.tuzhi.sys.web.controller;

import com.tuzhi.base.jpa.domain.Pager;
import com.tuzhi.base.jpa.healper.QueryHelper;
import com.tuzhi.base.service.BaseService;
import com.tuzhi.base.web.controller.SimpleBaseController;
import com.tuzhi.base.web.helper.AjaxResultHelper;
import com.tuzhi.base.web.vo.AjaxResultVO;
import com.tuzhi.base.web.vo.PageVO;
import com.tuzhi.common.domain.LoginUserBO;
import com.tuzhi.common.domain.ResultBO;
import com.tuzhi.common.exception.BusinessException;
import com.tuzhi.common.helper.ResultHelper;
import com.tuzhi.sys.jpa.entity.SysRoleEntity;
import com.tuzhi.sys.jpa.entity.SysUserEntity;
import com.tuzhi.sys.scurity.AuthService;
import com.tuzhi.sys.service.SysRoleService;
import com.tuzhi.util.StringTzUtil;
import com.tuzhi.util.UserContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import java.util.List;


@RestController
@RequestMapping("/sys/role")
@Api(tags = "角色")
@Slf4j
public class SysRoleController extends SimpleBaseController<SysRoleEntity, Long, Object> {


    @Autowired
    SysRoleService roleService;

    @Autowired
    AuthService authService;

    @Override
    protected BaseService getThisService() {
        return roleService;
    }

    @ApiOperation("启用/禁用角色")
    @PutMapping({"/updataStatus"})
    public ResultBO updataStatus(@RequestBody SysRoleEntity entity) {
        if (entity.getId() == null) {
            throw new BusinessException("id参数不能为空");
        }
        if (entity.getStatus()==null) {
            throw new BusinessException("状态不能为空");
        }
        roleService.save(entity);
        return AjaxResultHelper.success();
    }

    @ApiOperation("查询角色所有的用户")
    @PostMapping({"/getUser"})
    public ResultBO getUser(@RequestBody SysRoleEntity entity) {
        if (entity.getId() == null) {
            throw new BusinessException("id参数不能为空");
        }
        List<SysUserEntity> userList= roleService.getUserByRoleId(entity.getId());
        return AjaxResultHelper.success().setData(userList);
    }

    /**
     *
     * @param entity
     * @return
     */
    @ApiOperation("保存")
    @PutMapping({"/save"})
    @Override
    public ResultBO save(@RequestBody SysRoleEntity entity) {
        if (entity == null) {
            throw new BusinessException("参数不能为空");
        }
        if (StringUtils.isEmpty(entity.getName())) {
            throw new BusinessException("角色名不能为空");
        }
        roleService.save(entity);
        return AjaxResultHelper.success();
    }

    /**
     * 查询所有角色
     *
     * @return
     */
    @ApiOperation("查询所有角色")
    @GetMapping("/listRole")
    public AjaxResultVO listRole() {
        AjaxResultVO ajaxResultVO = AjaxResultHelper.success();
        ajaxResultVO.setData(roleService.listRoleEfficacious());
        return ajaxResultVO;
    }

    @ApiOperation("删除")
    @DeleteMapping({"del/{id}"})
    public ResultBO del(@PathVariable(value = "id", required = true) Long id) {
        authService.deleteRole(id);
        return ResultHelper.success();
    }

    @Override
    protected Pager<SysRoleEntity> pagerVoCreateWhere(PageVO<Object> pageVO, Pager<SysRoleEntity> pager) {
        LoginUserBO curLoginUser = UserContext.getUserInfo(true);
        QueryHelper.Builder<SysRoleEntity> queryBuilder = new QueryHelper.Builder<>();
        //超级管理员，查询所有的客户
        // 其它的只能查询到自己所在org的记录
        if (!curLoginUser.isSupper()) {
            queryBuilder.eq(SysRoleEntity::getId, 1L);
        }
        queryBuilder.sortDesc(SysRoleEntity::getId);
        pager.setWhereConditionBO(queryBuilder.build().getWhereConditionBO());
        return pager;
    }
}
